﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using LastoRevival.Service.Contracts.Models.Validation;
using mdryden.Core.Localization;
using mdryden.Core.Security;

namespace LastoRevival.Service.Contracts.Models
{
	public class CreateUserModel : IValidatableObject
	{


		[Required(ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "User_UsernameRequiredError")]
		[StringLength(StringLengthConstants.UsernameMaxLength, ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "StringLengthError")]
		public string UserName { get; set; }

		[Required(ErrorMessageResourceType=typeof(ValidationMessageLoader), ErrorMessageResourceName="User_EmailRequiredError")]
		[DataType(DataType.EmailAddress)]
		[StringLength(StringLengthConstants.EmailMaxLength, ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "StringLengthError")]
		public string Email { get; set; }

		[Required(ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "User_PasswordRequiredError")]
		[DataType(DataType.Password)]
		[StringLength(StringLengthConstants.PasswordMaxLength, ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "StringLengthError")]
		public string Password { get; set; }

		[Required(ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "User_PasswordConfirmationRequiredError")]
		[DataType(DataType.Password)]
		[Compare("Password", ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "User_PasswordsDoNotMatchError")]
		[StringLength(StringLengthConstants.PasswordMaxLength, ErrorMessageResourceType = typeof(ValidationMessageLoader), ErrorMessageResourceName = "StringLengthError")]
		public string ConfirmPassword { get; set; }

		// quick copy function
		public CreateUserModel Clone()
		{
			// don't forget that this is a shallow copy - it'll create reference copies
			// for objects
			return (CreateUserModel)this.MemberwiseClone();
		}


		public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			var errors = new List<ValidationResult>();

			if (!EmailValidator.IsValid(Email))
			{
				var error = new ValidationResult(LanguageLoader.GetWord("User_EmailInvalidError"), new[] { "Email" });
				errors.Add(error);
			}

			return errors;
		}
	}
}
